package praktikumid.w09.p2;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * Button panel on the left side, 
 * holds buttons.
 * @author Ago
 *
 */
public class ButtonPanel extends JPanel {
	
	/**
	 * Shortcut to the frame
	 */
	private MyFrame frame;
	
	/**
	 * Inner class to handle 
	 * "draw rectangle" button.
	 * @author Ago
	 *
	 */
	class DrawRectangleListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// here we use shortcut to access frame
			// and from there shortcut to access
			// drawing panel.
			frame.getDrawPanel().drawRectangle();
		}
		
	}
	

	/**
	 * Constructor. We use shortcut
	 * to the frame to simplify method
	 * calling.
	 * @param f Reference to the MyFrame instance.
	 */
	public ButtonPanel(MyFrame f) {
		frame = f;
		setBackground(Color.LIGHT_GRAY);
		
		// let's use box layout to put buttons
		// in to one column
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JButton bOval = new JButton("draw oval");
		JButton bRect = new JButton("draw rectangle");
		JButton bLine = new JButton("draw line");
		
		// we use 3 different ways to implement ActionListener
		
		// 1) separate class-file
		bOval.addActionListener(new DrawOvalListener());
		// 2) inner class
		bRect.addActionListener(new DrawRectangleListener());
		// 3) anonymous class 
		// (declaration and instantiation at the same time)
		bLine.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.getDrawPanel().drawLine();
				
			}
		});
		
		// let's add buttons to the panel
		add(bOval);
		add(bRect);
		add(bLine);
		
		
	}
}
